//
//  SortModel.m
//  project
//
//  Created by 黄建华 on 2019/1/17.
//  Copyright © 2019年 黄建华. All rights reserved.
//

#import "SortModel.h"

@implementation SortModel

- (instancetype)init {
    self = [super init];
    if (self) {
        _dateString        = [[NSDate date] dateToDayString];
        _user_id       = SN_USER.user_id;
    }
    return self;
}

+ (SortModel *)initWithDate:(NSDate *)date
{
    SortModel *model = [[SortModel alloc] init];
    model.user_id = SN_USER.user_id;
    model.dateString = [date dateToDayString];
    return model;
}

+ (SortModel *)getSortModelFromDBWithDayString:(NSString *)string
{
    NSString *where = [NSString stringWithFormat:@"user_id = '%@' and dateString = '%@'",
                       SN_USER.user_id, string];
    SortModel *model = [SortModel searchSingleWithWhere:where orderBy:nil];
    if (!model) {
        model               = [[SortModel alloc] init];
        model.dateString    = string;
        model.user_id       = SN_USER.user_id;
    }
    return model;
}

+ (void)updateModelWithDic:(id)object withDateString:(NSString *)date
{
    NSArray *sortStep = (NSArray *)object;
    SortModel *model = [SortModel getSortModelFromDBWithDayString:date];
    model.stepSort = [NSArray arrayWithArray:sortStep];
    [model updateToDB];
}

+ (void)updateModelWithSleepDurationDic:(id)object withDateString:(NSString *)date;
{
    NSArray *sleepDuations = (NSArray *)object;
    SortModel *model = [SortModel getSortModelFromDBWithDayString:date];
    model.sleepDurationSArray = [NSArray arrayWithArray:sleepDuations];
    [model updateToDB];
}
+ (void)updateModelWithSleepStartDic:(id)object withDateString:(NSString *)date
{
    NSArray *sleepStart = (NSArray *)object;
    SortModel *model = [SortModel getSortModelFromDBWithDayString:date];
    model.sleepStartArray = [NSArray arrayWithArray:sleepStart];
    [model updateToDB];
}

// 表名
+ (NSString *)getTableName {
    return @"SortModel";
}

// 复合主键
+ (NSArray *)getPrimaryKeyUnionArray {
    return @[@"user_id", @"dateString"];
}

// 表版本
+ (int)getTableVersion {
    return 1;
}

+ (void)initialize {
    [super initialize];
}

@end
